package com.dongway.netcondition.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dongway.core.common.Paginator;
import com.dongway.core.web.AbstractBaseController;
import com.dongway.netcondition.common.SystemPropertyCode;
import com.dongway.netcondition.dto.DevStateDTO;
import com.dongway.netcondition.dto.RegionDTO;
import com.dongway.netcondition.model.DevNetState;
import com.dongway.netcondition.model.SystemProperty;
import com.dongway.netcondition.service.DevNetStateService;
import com.dongway.netcondition.service.DevSelectHelper;
import com.dongway.netcondition.service.SystemPropertyService;
import com.dongway.netcondition.socketToBox.SocketContent;

@Controller
public class DetailController extends AbstractBaseController {

    @Autowired
    private SystemPropertyService propertyService;
    @Autowired
    private DevSelectHelper selectHelper;
    @Autowired
    private DevNetStateService netStateService;


    @RequestMapping("/detail.htm")
    public String detail(ModelMap modelMap) {
        List<SystemProperty> properties = propertyService.all();
        List<RegionDTO> regions = selectHelper.getRegionMapping();
        for (SystemProperty systemProperty : properties) {
            modelMap.put(systemProperty.getPName(), systemProperty.getPValue());
        }
        int size = SocketContent.getInstance().onlines().size();
        String jsonStr = JSON.toJSONString(regions);
        modelMap.put("regions", jsonStr);
        modelMap.put("connNum", size);
        return "net/detail";
    }


    @RequestMapping("/setKeepAlive.json")
    @ResponseBody
    public Object set(String s) {
        SystemProperty property;
        try {
            if (Integer.valueOf(s) > 300) {
                throw new NumberFormatException("只能填入小于或等于300的整数");
            }
            property = new SystemProperty();
            property.setPName(SystemPropertyCode.KEEP_ALIVE);
            property.setPValue(s);
        } catch (NumberFormatException e) {
            logger.error("只能填入小于或等于300的整数", e);
            return JsonFailed(e);
        } catch (Exception e) {
            logger.error("设置心跳错误", e);
            return JsonFailed(e);
        }
        return JsonSuccess(property);
    }


    @RequestMapping("/getOnlineNum.json")
    @ResponseBody
    public Object getOnlineNum() {
        try {
            List<Integer> ids = SocketContent.getInstance().onlines();
            int size = ids.size();
            return JsonSuccess(size);
        } catch (Exception e) {
            logger.error("获取信息错误", e);
            return JsonFailed(e);
        }
    }


    @RequiresRoles("admin")
    @RequestMapping("/detail/devRegion.htm")
    public String devRegion(Paginator paginator, ModelMap map) {
        try {
            List<DevStateDTO> devs = netStateService.allOrderByDevId(null);
            map.put("devs", devs);
            return "/net/detail/devRegion";
        } catch (Exception e) {
            logger.error("获取信息错误", e);
            return "获取信息错误";
        }
    }


    @RequiresRoles("admin")
    @RequestMapping("/detail/save.json")
    @ResponseBody
    public Object save(String dstr) {
        try {
            List<DevNetState> ds = JSON.parseArray(dstr, DevNetState.class);
            netStateService.batchUpdate(ds);
            return JsonSuccess("更新成功");
        } catch (Exception e) {
            logger.error("保存设备与区域信息错误", e);
            return JsonFailed(e);
        }
    }

}
